package com.five.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.api.dao.SystemAdviceMapper;
import com.five.api.service.SystemAdviceService;
import com.five.entity.SystemAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统通知表 服务实现类
 * </p>
 *
 * @author star
 * @since 2021-06-16
 */
@Service
public class SystemAdviceServiceImpl extends ServiceImpl<SystemAdviceMapper, SystemAdvice> implements SystemAdviceService {

    @Autowired
    private SystemAdviceMapper systemAdviceMapper;

    /**
     * 获取通知列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SystemAdvice> selectList(Long userId) {
        List<SystemAdvice> list = systemAdviceMapper.selectList(userId);
        //将通知改为已读状态
        SystemAdvice systemAdvice = new SystemAdvice();
        systemAdvice.setReadState(new Integer(1));
        //获取需要修改的用户
        QueryWrapper wrapper = new QueryWrapper();
        SystemAdvice wrapperSystemAdvice = new SystemAdvice();
        wrapperSystemAdvice.setUserId(userId);
        wrapper.setEntity(wrapperSystemAdvice);

        systemAdviceMapper.update(systemAdvice, wrapper);
        return list;
    }

    /**
     * 根据id查询是否有未读消息
     *
     * @param userId
     * @return
     */
    @Override
    public boolean getReadState(Long userId) {
        //获取所有消息
        List<SystemAdvice> list = systemAdviceMapper.selectList(userId);
        for (SystemAdvice systemAdvice : list) {
               //0为有未读消息
                if (systemAdvice.getReadState().equals(0)){
                    return true;
                }
        }
        return false;
    }
}
